package com.ybg.engine.mail;

import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.fastjson.JSON;
import com.ybg.util.LogUtil;
import com.ybg.util.SysParamUtil;

public class MailInfo {
	private String mailHost;
	private String mailPort;
	private String mailAuth;
	private String mailProtocol;
	private String mailAccount;
	private String mailPassword;
	private String from ;
	private String to ;
	
	public String getMailHost() {
		return mailHost;
	}
	public void setMailHost(String mailHost) {
		this.mailHost = mailHost;
	}
	public String getMailPort() {
		return mailPort;
	}
	public void setMailPort(String mailPort) {
		this.mailPort = mailPort;
	}
	public String getMailAuth() {
		return mailAuth;
	}
	public void setMailAuth(String mailAuth) {
		this.mailAuth = mailAuth;
	}
	public String getMailProtocol() {
		return mailProtocol;
	}
	public void setMailProtocol(String mailProtocol) {
		this.mailProtocol = mailProtocol;
	}
	public String getMailAccount() {
		return mailAccount;
	}
	public void setMailAccount(String mailAccount) {
		this.mailAccount = mailAccount;
	}
	public String getMailPassword() {
		return mailPassword;
	}
	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	
	public static MailInfo getMailInfo(JdbcTemplate jdbcTemplate){
		MailInfo mailInfo = new MailInfo();
    	Object ob = SysParamUtil.getNewSimpleConfigValue(jdbcTemplate,Constants.MAIL_PARAM);
    	if(ob == null){
    		LogUtil.error("can't find the email param !!! use default param");
    		mailInfo = getDefaultMailInfo();
    		return mailInfo;
    	}
    	
    	String json = (String)ob;
		try{
			mailInfo = JSON.parseObject(json, MailInfo.class);
		}catch(Exception e){
			LogUtil.error("格式化邮箱配置信息失败", e);
			mailInfo = getDefaultMailInfo();
		}
		return mailInfo;
	}
	
	private static MailInfo getDefaultMailInfo(){
		MailInfo mailInfo = new MailInfo();
		mailInfo.setMailHost("smtp.qq.com");
		mailInfo.setMailAuth("true");
		mailInfo.setMailProtocol("smtp");
		mailInfo.setMailAccount("xxts@yibaogao.com");
		mailInfo.setMailPassword("YBG123456ybg");
		mailInfo.setFrom("xxts@yibaogao.com");
		mailInfo.setTo("ningjiahong@yibaogao.com,zhouqixin@yibaogao.com,liliping@yibaogao.com,lihan@yibaogao.com");
		return mailInfo;
	}
}
